package com.zuimei.zuimei.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.beans.FindAppItem1Bean;
import com.zuimei.zuimei.utils.ImgUtil;
import com.zuimei.zuimei.utils.ScreenUtil;

import java.util.List;

/**
 * Created by Jogger on 2017/1/17.
 * 发现页recycleview条目适配器
 */

public class Item3RecycleViewItemAdapter extends RecyclerView.Adapter<Item3RecycleViewItemAdapter
        .MyViewHolder> {

    private List<FindAppItem1Bean.AppsBean> mAppList;
    private Context mContext;
    private OnItemClickListener mListener;

    public interface OnItemClickListener{
        void onItemClick(View view, FindAppItem1Bean.AppsBean appBean);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        mListener=listener;
    }

    public Item3RecycleViewItemAdapter(Context context) {
        mContext = context;
    }

    public void loadData(List<FindAppItem1Bean.AppsBean> appList) {
        mAppList = appList;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.rv_item1_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Object o = mAppList.get(position);
        final FindAppItem1Bean.AppsBean appBean = (FindAppItem1Bean.AppsBean) o;
        Picasso.with(mContext)
                .load(appBean.getIcon_image())
                .config(Bitmap.Config.RGB_565)
                .into(holder.iv_head);
        holder.tv_title.setText((appBean).getTitle());
        holder.tv_sub_title.setText((appBean).getSub_title());
        Picasso.with(mContext)
                .load((appBean).getCover_image())
                .config(Bitmap.Config.RGB_565)
                .transform(ImgUtil.getTransformation(ScreenUtil.getSrceenWidth(mContext)))
                .into(holder.iv_center);
        String tags = "";
        for (int j = 0; j < (appBean).getTags().size(); j++) {
            tags += "#" + appBean.getTags().get(j);
        }
        holder.tv_tags.setText(tags);
        holder.tv_digest.setText((appBean).getDigest());
        holder.tv_up_times.setText(appBean.getUp_times() + "");
        holder.ll_content_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener!=null){
                    mListener.onItemClick(view,appBean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mAppList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        ImageView iv_head;
        TextView tv_sub_title;
        ImageView iv_center;
        TextView tv_digest;
        TextView tv_tags;
        TextView tv_up_times;
        LinearLayout ll_content_container;

        MyViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            iv_head = (ImageView) itemView.findViewById(R.id.iv_head);
            tv_sub_title = (TextView) itemView.findViewById(R.id.tv_sub_title);
            iv_center = (ImageView) itemView.findViewById(R.id.iv_center);
            tv_digest = (TextView) itemView.findViewById(R.id.tv_digest);
            tv_tags = (TextView) itemView.findViewById(R.id.tv_tags);
            tv_up_times = (TextView) itemView.findViewById(R.id.tv_up_times);
            ll_content_container= (LinearLayout) itemView.findViewById(R.id.ll_content_container);
        }
    }

}
